//
//  MainPageViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  首页轮播图

import UIKit
import WebKit


class MainPageViewController : WKBaseViewController {

    //修补所用的view
    var compassView : UIView = UIView()
    
    
    //设置导航栏样式
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false


//        if mainIndexArray.count > 0 {
//            mainIndexArray.removeAllObjects()
//        }
        
        view.bringSubview(toFront: (self.navigationController?.navigationBar)!)
        
        //若导航栏的子页面数量大于1，则设置第一面的导航栏颜色为白色，其他为橘色
        if (self.navigationController?.viewControllers.count)! > 1 {
            
            self.navigationController?.navigationBar.barTintColor = UIColor.white
            //文字颜色
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black]
            
            UIApplication.shared.statusBarStyle = .default
            
        } else {

            compassView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SW, height: 20))
            compassView.backgroundColor = commonBtnColor
            view.addSubview(compassView)
            self.navigationController?.navigationBar.isHidden = true
            
            self.webView.frame = CGRect.init(x: 0, y: 20, width: SW, height: SH - 64)
            
            ///状态栏背景色
            self.navigationController?.navigationBar.barTintColor = commonBtnColor
            
            
            //文字颜色
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
            
            UIApplication.shared.statusBarStyle = .lightContent
        }
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()


        //从存跳转链接的数组中获取最后一个链接，最后当前页面加载的链接
//        if mainIndexArray.count > 1 {
//            //取出数组的url转为字符串
////            url = mainIndexArray.lastObject as! String
//            
//            //XFLog(message: url)
//            
//            webView.load(URLRequest.init(url: URL.init(string: self.url)!))
//        } else {
        
        if self.webView == nil {
            return
        }
        
        if (self.navigationController?.childViewControllers.count)! > 1 {
            
            DispatchQueue.main.async {
                
                
                //判断是否包含域名
                if self.url.contains(comStrURL) {

                    //根据是否有网络状态加载断网图片
                    StatusBarCheck.shared.networkingStatesFromStatebar { (result) in
                        
                        if result == "notReachable" {
                            DispatchQueue.main.async {
                                
                                
                                self.compassView.frame = CGRect.init(x: 0, y: 0, width: SW, height: 64)
                                
                                self.urlRequestCache = NSURLRequest.init(url: URL.init(string: self.url)!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
                            }
                            
                        } else {
                            DispatchQueue.main.async {
                                
                                
                                self.compassView.frame = CGRect.init(x: 0, y: 0, width: SW, height: 20)
                                
                                self.urlRequestCache = NSURLRequest(url: URL.init(string: self.url)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10)
                                
                                self.webView.load(self.urlRequestCache as URLRequest)
                                
                            }
                        }
                    }
                    
                    
                } else {
                    CustomAlertView.shared.alertWithTitle(strTitle: "请下拉刷新")
                }
            }
        
        } else {
            let urlStr = "http://\(comStrURL)/app/index.aspx?devtype=1" + "&token=" + self.token
            
            StatusBarCheck.shared.networkingStatesFromStatebar(_com: { (result) in
                if result == "notReachable" {
                    self.compassView.frame = CGRect.init(x: 0, y: 0, width: SW, height: 64)
                    self.urlRequestCache = NSURLRequest.init(url: URL.init(string: urlStr)!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 0)
                    
                    self.webView.load(self.urlRequestCache as URLRequest)
                } else {
                    if urlStr.contains(comStrURL) {
                        self.compassView.frame = CGRect.init(x: 0, y: 0, width: SW, height: 20)
                        self.urlRequestCache = NSURLRequest.init(url: URL.init(string: urlStr)!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 0)
                        
                        self.webView.load(self.urlRequestCache as URLRequest)
                    } else {}
                }
            })
        }
    }
    
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
//        XFLog(message: self.url)
        
        
        if navigationAction.navigationType == WKNavigationType.linkActivated || self.url.contains("product_list.aspx") || self.url.contains("&tp=buynow") {
            
            if self.url.contains("?") {
                self.url = self.url + ("&devtype=1&token=") + (token)
            } else {
                self.url = self.url + ("?devtype=1&token=") + (token)
            }

            //判断是否包含相同的url
            if mainIndexArray.count > 0 && (mainIndexArray.lastObject as! String) == self.url {
                self.webView.reload()
                decisionHandler(.allow)

                mainIndexArray.removeLastObject()
                return
            
            }
            
            mainIndexArray.add(self.url)
            self.aaa(str:self.url)

            decisionHandler(.cancel)
    
            
        } else {
            
            
            decisionHandler(.allow)
        }

    }
    
    
    /// 页面跳转
    ///
    /// - Parameter str: 跳转的链接
    func aaa(str : String) -> Void {
        
        let vvv = MainPageViewReplaceVIew()
        vvv.url = str

        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vvv, animated: true)
        self.hidesBottomBarWhenPushed = false
        
    }

}
